package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ControllerBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.glide.GlideApp;

/**
 *    主机adapter样式
 */
public final class SmartControllerAdapter extends AppAdapter<ControllerBean> {

    private int selectPosition;

    public SmartControllerAdapter(@NonNull Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final View contentView;
        private final ImageView iconController;
        private final TextView tvControllerName;
        private final TextView tvControllerId;
        private final TextView tvControllerStatus;
        private final View btnSetting;

        private ViewHolder() {
            super(R.layout.item_controller);
            contentView = findViewById(R.id.content_view);
            iconController = findViewById(R.id.icon_controller);
            tvControllerName = findViewById(R.id.tv_controller_name);
            tvControllerId = findViewById(R.id.tv_controller_id);
            tvControllerStatus = findViewById(R.id.tv_controller_status);
            btnSetting = findViewById(R.id.btn_setting);
        }

        @Override
        public void onBindView(int position) {
            if(selectPosition == position){
                contentView.setSelected(true);
            }else{
                contentView.setSelected(false);
            }
            ControllerBean item = getItem(position);
            RecyclerView.LayoutParams rl = (RecyclerView.LayoutParams) itemView.getLayoutParams();
            if(getData().size() == 1){
                rl.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
                rl.rightMargin = getResources().getDimensionPixelSize(R.dimen.dp_4);
            }
            itemView.setLayoutParams(rl);
            GlideApp.with(iconController).load(item.getImageUrl()).into(iconController);
            tvControllerName.setText(item.getName());
            tvControllerId.setText(item.getCodeNo());
            //在线状态：离线 = 0,在线 = 1,未知 = 2,网络异常 = 3
            switch (item.getStateType()){
                case 0:
                    tvControllerStatus.setText(R.string.offline);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setSelected(false);
                    break;
                case 1:
                    tvControllerStatus.setText(R.string.online);
                    tvControllerStatus.setEnabled(true);
                    tvControllerStatus.setSelected(false);
                    break;
                case 2:
                    tvControllerStatus.setText(R.string.fault);
                    tvControllerStatus.setEnabled(false);
                    tvControllerStatus.setSelected(false);
                    break;
            }
        }
    }

    @Override
    public long getItemId(int position) {
        return getData().get(position).hashCode();
    }

    public void select(int position){
        selectPosition = position;
        notifyDataSetChanged();
    }
}